package com.c6906.vdam.das.controller;

import com.c6906.vdam.das.config.redis.RedisUtils;
import com.c6906.vdam.das.model.dto.ResultInfo;
import com.c6906.vdam.das.model.dto.ResultUtil;
import com.c6906.vdam.das.model.entity.TerminalBusinessInfo;
import com.c6906.vdam.das.model.entity.TerminalData;
import com.c6906.vdam.das.model.entity.TerminalLineData;
import com.c6906.vdam.das.service.TerminalHistoryDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author : TAOHUIQI @Date: 2024/6/15 20:49 @Description:
 */
@RestController
@RequestMapping({"/terminal/history"})
@Api(description = "设备异常和告警处理Controller")
public class TerminalHistoryController {

    private static final Logger logger = LoggerFactory.getLogger(TerminalHistoryController.class);

    @Autowired
    TerminalHistoryDetailsService terminalHistoryDetailsService;

    @Autowired
    RedisUtils redisUtils;

    @PostMapping({"/handleTerminalHistoryDetails"})
    @ApiOperation(value = "处理设备历史异常和告警详情数据", notes = "处理设备历史异常和告警详情数据")
    public ResultInfo<String> handleTerminalHistoryDetails(HttpServletRequest request,
                                                           @RequestBody List<TerminalData> dataList) {
        // 填充基本信息
        for (TerminalData terminalData : dataList) {
            // 根据设备编号判断redis是否存在
            TerminalBusinessInfo terminalBusinessInfo = redisUtils
                    .terminalInfoIsExistFormRedis("SN" + terminalData.getTerminalSn());
            // logger.info("根据设备编号从redis获取基本信息为={}",
            // JSON.toJSONString(terminalBusinessInfo));
            if (null != terminalBusinessInfo && !StringUtils.isEmpty(terminalBusinessInfo.getTerminalSn())) {
                // 填充DM数据设备基本信息
                generateTerminalData(terminalData, terminalBusinessInfo, terminalData.getTerminalSn());
            }
        }
        // 保存两条最新的数据到redis
        redisUtils.saveLastDatasForHistory(2, dataList);
        terminalHistoryDetailsService.handleTerminalHistoryDetails(dataList);
        return ResultUtil.getSuccessResult("SUCCESS");
    }

    @PostMapping({"/handleTerminalHistoryOnlineDetails"})
    @ApiOperation(value = "处理设备历史异常数据-在线离线", notes = "处理设备历史异常数据-在线离线")
    public ResultInfo<String> handleTerminalHistoryOnlineDetails(HttpServletRequest request,
                                                                 @RequestBody List<TerminalLineData> dataList) {
        // 填充基本信息
        for (TerminalLineData terminalData : dataList) {
            // 根据设备编号判断redis是否存在
            TerminalBusinessInfo terminalBusinessInfo = redisUtils
                    .terminalInfoIsExistFormRedis("SN" + terminalData.getTerminalSn());
            // logger.info("根据设备编号从redis获取基本信息为={}",
            // JSON.toJSONString(terminalBusinessInfo));
            if (null != terminalBusinessInfo && !StringUtils.isEmpty(terminalBusinessInfo.getTerminalSn())) {
                // 填充DM数据设备基本信息
                generateTerminalLineData(terminalData, terminalBusinessInfo, terminalData.getTerminalSn());
                terminalHistoryDetailsService.handleTerminalHistoryOnlineDetails(terminalData);
            }
        }
        return ResultUtil.getSuccessResult("SUCCESS");
    }

    private TerminalData generateTerminalData(TerminalData terminalData, TerminalBusinessInfo terminalBusinessInfo,
                                              String terminalSn) {
        if (StringUtils.isEmpty(terminalBusinessInfo.getVehPlateNum())) {
            logger.error("设备{}的车牌为空，数据无法入库...", terminalSn);
            terminalData.setInsertFlag(1);
        } else if (StringUtils.isEmpty(terminalBusinessInfo.getVehiclesCode())) {
            logger.error("设备{}的车辆编码为空，数据无法入库...", terminalSn);
            terminalData.setInsertFlag(1);
        } else {
            terminalData.setRegionCode(terminalBusinessInfo.getRegionCode());
            terminalData.setRegionName(terminalBusinessInfo.getRegionName());
            terminalData.setVehiclesCode(terminalBusinessInfo.getVehiclesCode());
            terminalData.setVehPlateNum(terminalBusinessInfo.getVehPlateNum());
            terminalData.setOwnUnitId(terminalBusinessInfo.getOwnUnitId());
            terminalData.setOwnUnitName(terminalBusinessInfo.getOwnUnitName());
            terminalData.setUseUnitId(terminalBusinessInfo.getUseUnitId());
            terminalData.setUseUnitName(terminalBusinessInfo.getUseUnitName());
            terminalData.setVehUseType(terminalBusinessInfo.getVehUseType());
            terminalData.setVehOwnType(terminalBusinessInfo.getVehOwnType());
            terminalData.setVehPropId(terminalBusinessInfo.getVehPropId());
            terminalData.setPlateColorName(terminalBusinessInfo.getPlateColorName());
            terminalData.setInsertFlag(0);
        }
        return terminalData;
    }

    private TerminalLineData generateTerminalLineData(TerminalLineData terminalData,
                                                      TerminalBusinessInfo terminalBusinessInfo, String terminalSn) {
        if (StringUtils.isEmpty(terminalBusinessInfo.getVehPlateNum())) {
            logger.error("设备{}的车牌为空，数据无法入库...", terminalSn);
            terminalData.setInsertFlag(1);
        } else if (StringUtils.isEmpty(terminalBusinessInfo.getVehiclesCode())) {
            logger.error("设备{}的车辆编码为空，数据无法入库...", terminalSn);
            terminalData.setInsertFlag(1);
        } else {
            terminalData.setRegionCode(terminalBusinessInfo.getRegionCode());
            terminalData.setRegionName(terminalBusinessInfo.getRegionName());
            terminalData.setVehiclesCode(terminalBusinessInfo.getVehiclesCode());
            terminalData.setVehPlateNum(terminalBusinessInfo.getVehPlateNum());
            terminalData.setOwnUnitId(terminalBusinessInfo.getOwnUnitId());
            terminalData.setOwnUnitName(terminalBusinessInfo.getOwnUnitName());
            terminalData.setUseUnitId(terminalBusinessInfo.getUseUnitId());
            terminalData.setUseUnitName(terminalBusinessInfo.getUseUnitName());
            terminalData.setVehUseType(terminalBusinessInfo.getVehUseType());
            terminalData.setVehOwnType(terminalBusinessInfo.getVehOwnType());
            terminalData.setVehPropId(terminalBusinessInfo.getVehPropId());
            terminalData.setPlateColorName(terminalBusinessInfo.getPlateColorName());
            terminalData.setInsertFlag(0);
        }
        return terminalData;
    }
}
